package net.jxvtc.blogv1.controller;

import net.jxvtc.blogv1.domain.Comment;
import net.jxvtc.blogv1.domain.User;
import net.jxvtc.blogv1.service.CommentService;
import net.jxvtc.blogv1.service.UserService;
import net.jxvtc.blogv1.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @program: blogv1
 * @description: 评论控制器
 * @author: 蓝建平
 * @create: 2020-10-24 10:52
 **/
@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 功能：按作者，进行分页模糊查询
     * @param async  是否异步，默认不是异步，即false, 如果异步，则采用ajax方式加载数据
     * @param pageIndex  第几页，默认为第0页
     * @param pageSize   每页几条记录，默认为每页10条
     * @param author      查询的关键字  （用户名） 如：用户名为 张，则 LIKE '%张%'
     * @param model    通过model向页面传递数据。
     * @return   ModelAndView
     *          包括模型和视图，视图指定要跳转的页面或把数据加载到页面的某个部分
     *                       模型表示向页面传递的数据
     */
    @GetMapping
    public ModelAndView list(@RequestParam(value = "async", required = false) boolean async,
                             @RequestParam(value = "pageIndex", required = false, defaultValue = "0") int pageIndex,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
                             @RequestParam(value = "author", required = false, defaultValue = "") String author, Model model) {

        Pageable pageable = PageRequest.of(pageIndex, pageSize);
        Page<Comment> page = commentService.listCommentsByAuthorLike(author,pageable);
        List<Comment> list = page.getContent(); // 当前所在页面数据列表
        for (Comment comment : list) {
            System.out.println(comment.getAuthor());
        }
        model.addAttribute("page", page);
        model.addAttribute("commentList", list);
        return new ModelAndView(async == true ? "comments/list :: #mainContainerRepleace" : "comments/list", "commentModel",
                model);
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Response> delete(@PathVariable("id") Integer id) {
        try
        {
            commentService.removeComment(id);
        }catch (Exception ex)
        {
            return ResponseEntity.ok().body(new Response(false,ex.getMessage()));
        }
        return ResponseEntity.ok().body(new Response(true,"删除成功！"));

    }

    /**
     * 获取创建表单页面
     * @param model
     * @return
     */
    @GetMapping("/add")
    public ModelAndView createForm(Model model) {
        model.addAttribute("comment", new Comment());
        return new ModelAndView("comments/add", "commentModel", model);
    }


    /**
     * 保存或者修改评论
     *
     * @param user
     * @return
     */
    @PostMapping
    public ResponseEntity<Response> saveOrUpateComment(Comment comment) {

        try {
            commentService.saveOrUpate(comment);
        } catch (Exception e) {
            return ResponseEntity.ok().body(new Response(false, e.getMessage()));
        }

        return ResponseEntity.ok().body(new Response(true, "处理成功", comment));
    }

    /**
     * 获取修改用户的界面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping(value = "edit/{id}")
    public ModelAndView modifyForm(@PathVariable("id") Integer id, Model model) {
       Comment comment = commentService.getCommentById(id);
        model.addAttribute("comment", comment);
        return new ModelAndView("comments/edit", "commentModel", model);
    }

}
